import java.util.*;
import java.io.*;

public class Grammar {

    private List<Rule> rules = new ArrayList<Rule>();
    private Set<Symbol> symbols = new HashSet<Symbol>(); 
    private Symbol startSymbol;
    
    
    public List<Rule> rules() {
       return rules;
    }
    
    public Set<Symbol> symbols() {
       return symbols;
    }
    
    public Symbol startSymbol() {
       return startSymbol;
    }

    public void addRule(Rule r) {
       rules.add(r);
       symbols.add(r.lhs());
       for (Symbol s : r.rhs()) {
           symbols.add(s);
       }
       // first rule indicates what the start symbol is
       if (startSymbol == null) {
           startSymbol = r.lhs();
       }
    }
    
    public String toString() {
       String str = "";
       for (Rule r : rules) {
          str = str + r+"\n";
       }
       return str;
    }
   
}